package cn.xgj.sfzy.service.impl;

import cn.xgj.sfzy.entity.Course;
import cn.xgj.sfzy.entity.CourseEvaluation;
import cn.xgj.sfzy.entity.User;
import cn.xgj.sfzy.mapper.CourseEvaluationMapper;
import cn.xgj.sfzy.mapper.CourseMapper;
import cn.xgj.sfzy.mapper.UserMapper;
import cn.xgj.sfzy.service.CourseEvaluationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName CourseEvaluationServiceImpl
 * @Description TODO
 * @Author xgj
 * @Date 2024年10月24日 上午10:13
 * @Version 1.0
 */
@Service
public class CourseEvaluationServiceImpl implements CourseEvaluationService {
    @Autowired
    private CourseEvaluationMapper courseEvaluationMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CourseMapper courseMapper;

    @Override
    public int add(CourseEvaluation courseEvaluation) {
        return courseEvaluationMapper.insert(courseEvaluation);
    }

    @Override
    public int edit(CourseEvaluation courseEvaluation) {
        return courseEvaluationMapper.update(courseEvaluation);
    }

    @Override
    public int remove(Integer id) {
        return courseEvaluationMapper.delete(id);
    }

    @Override
    public List<CourseEvaluation> findAll(CourseEvaluation courseEvaluation) {
        return courseEvaluationMapper.find(courseEvaluation);
    }

    @Override
    public CourseEvaluation findById(Integer id) {
        return courseEvaluationMapper.findById(id);
    }
//根据课程id查询
    @Override
    public List<CourseEvaluation> findByCourseId(Integer courseId) {
        List<CourseEvaluation> courseEvaluationList = courseEvaluationMapper.findByCourseId(courseId);
        for (CourseEvaluation courseEvaluation : courseEvaluationList) {
            User user = userMapper.findById(Math.toIntExact(courseEvaluation.getUserId()));
            courseEvaluation.setUser(user);
        }
        return courseEvaluationList;
    }

    @Override
    public PageInfo<CourseEvaluation> findByPage(Integer pageNum, Integer ageSize, CourseEvaluation courseEvaluation) {
        PageHelper.startPage(pageNum, ageSize);
        List<CourseEvaluation> list = courseEvaluationMapper.find(courseEvaluation);
        PageInfo<CourseEvaluation> pageInfo = new PageInfo<CourseEvaluation>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<CourseEvaluation> findByConsultId(Integer pageNum, Integer ageSize,Integer consultId) {
        PageHelper.startPage(pageNum, ageSize);
        List<Integer> list = courseMapper.findIdByConsultId(consultId);
        List<CourseEvaluation> evList=new ArrayList<CourseEvaluation>();
        for (Integer id : list){
            List<CourseEvaluation> evaluations = courseEvaluationMapper.findByCourse(id);
                evList.addAll(evaluations);
                for (CourseEvaluation ev:evaluations){
                    Integer userId = ev.getUserId().intValue();
                    User byId = userMapper.findById(userId);
                    ev.setUser(byId);
                }
        }
        PageInfo<CourseEvaluation> Info = new PageInfo<CourseEvaluation>(evList);
        return Info;
    }
}
